package com.body.park.user.bloom;

import com.body.park.user.config.RedisSonConfiguration;
import com.body.park.user.entity.TUser;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBloomFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author chaoxian.wu
 * @DATE 2022/4/21 9:01
 */
@Slf4j
@Component
public class InitBloomFilterData implements ApplicationRunner {

    @Autowired
    private RedisSonConfiguration redisSonConfiguration;

    private RBloomFilter<Long> bloomFilter;

    @PostConstruct
    public void init() {
        bloomFilter = redisSonConfiguration.bloomFilter();
    }

    /**
     *
     * @param args
     * @throws Exception
     */
    @Override
    public void run(ApplicationArguments args) throws Exception {
        List<TUser> userList = new ArrayList<>();
        //模拟数据
        for (long i = 0; i < 1000L; i++) {
            TUser user = new TUser();
            user.setId(i);
            userList.add(user);
        }
        userList.forEach(tUser -> {
            bloomFilter.add(tUser.getId());
        });
    }
}
